<?php
/**
 * ShareController class.
 * ShareController is for social share API implementation.
 * 
 * LBB Solutions Ltd. 版权所有 @ 2013.
 */
class ShareController extends ApiController
{
	/**
	 * Share special to user feed
	 */
	public function actionSpecial()
	{
		$site_id = Yii::app()->request->getParam('site_id');
		if (empty($site_id)) $this->_sendError('The parameter "site_id" is missing!');
		
		$site_token = Yii::app()->request->getParam('site_token');
		if (empty($site_token)) $this->_sendError(BUtils::T('no_login_share_special_msg'));
		
		$id_special = Yii::app()->request->getParam('id');
		if (empty($id_special)) $this->_sendError('The parameter "id" is missing!');
		
		//fetch special info for sharing
		$sql_special = 'SELECT s.id, s.id_merchant, s.url_picture, s.content, s.start_hour, s.end_hour, s.price, s.value, s.discount,
			  m.name, m.address, m.biz_area, m.url_offer_img, m.currency_code
			FROM tbl_merchant_special s
			INNER JOIN tbl_merchant_account m ON m.id=s.id_merchant
			WHERE s.id=:id_special ';
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql_special);
		$row = $command->queryRow(true, array(':id_special'=>$id_special));
		$connection->active = false;
		
		if (empty($row['id'])) {
			$hid = Yii::app()->request->getParam('hid', 'none');
			$this->APILogging('not special sharing', 'SID: ' . $id_special, $hid);
			Yii::app()->end();
		}
		
		// make sharing data
		$og_message = BUtils::T('special_og_message_lbl');
		$og_name = BUtils::T('special_og_name_lbl', array('{merchant}' => $row['name']));
		$og_description = BUtils::T('special_og_description_lbl', array(
				'{content}' => $row['content'],
				'{discount}' => sprintf('%d', $row['discount'] * 100) . '%',
				'{end_time}' => BUtils::GetEndHourFormat($row['end_hour']),
			)
		);
		$og_url_thumb = 'http://yamii.fi/img/logo.icon.72.png';
		if (!empty($row['url_offer_img'])) {
			$og_url_thumb = 'http://yamii.fi/business' . $row['url_offer_img'];
		}
		$og_link = 'http://yamii.fi/special/' . $id_special;
		
		if ($site_id == 1) {
			// facebook sharing
			$facebook = $this->_getFacebookObject($site_token);
			$attachment = array(
				'message' => $og_message,			// user's message
				'name' => $og_name,					// the special link name
				'link' => $og_link,					// the special link url (to web special link)
				'caption' => 'yamii.fi',
				'description' => $og_description,	// the special description
				'picture' => $og_url_thumb,			// the special picture (90x90)
			);
			try {
				//get user id
				$hid = Yii::app()->request->getParam('hid');
				$key = Yii::app()->request->getParam('key');
				if (empty($hid) or empty($key)) {
					$this->_sendJSON(300, BUtils::T('no_login_share_special_msg'));
				}
				$id = $this->_auth($hid, $key);
				
				$params = array(
					':id_user' => $id,
					':id_special' => $id_special,
				);

				$connection = Yii::app()->db;
				$sql_share = 'SELECT insert_timestamp FROM tbl_merchant_special_shared WHERE id_user=:id_user AND id_special=:id_special LIMIT 1 ';
				$sql_insert = 'INSERT INTO tbl_merchant_special_shared (id_user, id_special) VALUES (:id_user, :id_special) ';
				$command = $connection->createCommand($sql_share);
				$row = $command->queryRow(true, $params);
				if (empty($row['insert_timestamp'])) {
					$result = $facebook->api('/me/feed/', 'post', $attachment);
					//record this sharing
					if (!empty($result)) {
						$command = $connection->createCommand($sql_insert);
						$command->execute($params);
					}
				}
				$connection->active = false;
				
				$this->_sendJSON(200, 'OK');
			} catch (FacebookApiException $ex) {
				$connection->active = false;
				$this->_sendError($ex->getMessage());
			}
		}
	}
}